<template>
  <van-nav-bar :title="navTitle" v-bind="$attrs" :fixed="fixed" v-on="$listeners">
    <template slot="right">
      <slot name="right"></slot>
    </template>
    <template slot="left">
      <slot name="left"></slot>
    </template>
    <template slot="title">
      <slot name="title"></slot>
    </template>
  </van-nav-bar>
</template>
<script>
export default {
  name: 'NavBar',
  props: {
    title: {
      type: String,
      default: '',
    },
    fixed: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      /* 导航栏标签文本 */
      navTitle: '',
    }
  },

  mounted() {
    this.init()
  },

  methods: {
    // 初始化数据
    init() {
      this.initNavBarCenterText()
    },
    /* 初始化导航栏中间文本 */
    initNavBarCenterText() {
      // 中间文本有传值则优先赋值，如没有值则取路由的标签名
      if (this.title) {
        this.navTitle = this.title
        return
      } else {
        if (this.$route.meta.title) {
          this.navTitle = String(this.$route.meta.title)
        }
      }
    },
  },
}
</script>
